import { Controller, Post, Delete, Get, Param, Request, UseGuards } from '@nestjs/common';
import { LikeService } from './like.service';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';

@ApiTags('点赞管理')
@Controller('api/likes')
export class LikeController {
  constructor(private readonly likeService: LikeService) {}

  // 点赞文章（需要登录）
  @Post('article/:articleId')
  @ApiOperation({
    summary: '点赞文章',
    description: '为指定文章添加点赞',
  })
  @ApiParam({
    name: 'articleId',
    description: '文章ID',
    example: '1',
  })
  @ApiResponse({
    status: 201,
    description: '点赞成功',
  })
  @ApiResponse({
    status: 400,
    description: '已点赞或文章不存在',
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  createLike(@Request() req, @Param('articleId') articleId: number) {
    const userId = req.user.id;
    return this.likeService.createLike(userId, articleId);
  }

  // 取消点赞（需要登录）
  @Delete('article/:articleId')
  @ApiOperation({
    summary: '取消点赞',
    description: '取消对指定文章的点赞',
  })
  @ApiParam({
    name: 'articleId',
    description: '文章ID',
    example: '1',
  })
  @ApiResponse({
    status: 204,
    description: '取消成功',
  })
  @ApiResponse({
    status: 400,
    description: '未点赞或文章不存在',
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  removeLike(@Request() req, @Param('articleId') articleId: number) {
    const userId = req.user.id;
    return this.likeService.removeLike(userId, articleId);
  }

  // 切换点赞状态（需要登录）
  @Post('toggle/:articleId')
  @ApiOperation({
    summary: '切换点赞状态',
    description: '切换对指定文章的点赞状态（点赞/取消点赞）',
  })
  @ApiParam({
    name: 'articleId',
    description: '文章ID',
    example: '1',
  })
  @ApiResponse({
    status: 200,
    description: '切换成功',
  })
  @ApiResponse({
    status: 404,
    description: '文章不存在',
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  toggleLike(@Request() req, @Param('articleId') articleId: number) {
    const userId = req.user.id;
    return this.likeService.toggleLike(userId, articleId);
  }

  // 获取当前用户的点赞列表（需要登录）
  @Get('user')
  @ApiOperation({
    summary: '获取用户点赞列表',
    description: '获取当前登录用户的所有点赞记录',
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  getUserLikes(@Request() req) {
    const userId = req.user.id;
    return this.likeService.getUserLikes(userId);
  }

  // 检查当前用户是否点赞了指定文章（需要登录）
  @Get('check/:articleId')
  @ApiOperation({
    summary: '检查点赞状态',
    description: '检查当前用户是否已点赞指定文章',
  })
  @ApiParam({
    name: 'articleId',
    description: '文章ID',
    example: '1',
  })
  @ApiResponse({
    status: 200,
    description: '检查成功',
    schema: {
      example: { isLiked: true },
    },
  })
  @ApiResponse({
    status: 401,
    description: '用户未登录',
  })
  async isArticleLiked(@Request() req, @Param('articleId') articleId: number) {
    const userId = req.user.id;
    const isLiked = await this.likeService.isArticleLikedByUser(userId, articleId);
    return { isLiked };
  }
}
